import { ethers } from 'ethers'
import { WEB3_PROVIDER_HTTPS } from 'configs'

const rpcUrl = WEB3_PROVIDER_HTTPS

const getWeb3Provider = () => {
  if (window.ethereum) {
    return new ethers.providers.Web3Provider(window.ethereum)
  } else {
    return null
  }
}

const getJsonRpcProvider = () => {
  return new ethers.providers.JsonRpcProvider(rpcUrl)
}

const getProvider = () => {
  var provider = getWeb3Provider()
  if (provider) {
    return provider
  } else {
    return getJsonRpcProvider()
  }
}

const getProviderFromAction = (action) => {
  return new ethers.providers.Web3Provider(action.web3.currentProvider)
}

export {
  getWeb3Provider,
  getJsonRpcProvider,
  getProvider,
  getProviderFromAction,
}
